package com.ruoyi.web.controller.dsitem;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.convert.DsitemConvert;
import com.ruoyi.domain.Dsitem;
import com.ruoyi.domain.DsitemSubList;
import com.ruoyi.dto.DsitemDto;
import com.ruoyi.service.IDsitemService;
import com.ruoyi.service.IDsitemSubListService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作品Controller
 * 
 * @author ruoyi
 * @date 2024-05-31
 */
@Controller
@RequestMapping("/biz/dsitem")
public class DsitemController extends BaseController
{

    @Autowired
    private IDsitemService dsitemService;

    @Autowired
    private IDsitemSubListService dsitemSubListService;

    /**
     * 查询作品列表
     */
    @RequiresPermissions("system:dsitem:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DsitemDto dsitemDto)
    {
        startPage();
        List<DsitemDto> list = dsitemService.selectDsitemList(dsitemDto);
        for(DsitemDto dto : list) {
            List<DsitemSubList> dsitemSubList = dsitemSubListService.selectDsitemSubListBySubId(dto.getItemId());
            dto.setDsitemSubLists(dsitemSubList);
        }

        return getDataTable(list);
    }

    /**
     * 导出作品列表
     */
    @RequiresPermissions("system:dsitem:export")
    @Log(title = "作品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DsitemDto dsitemDto)
    {
        List<DsitemDto> list = dsitemService.selectDsitemList(dsitemDto);
        ExcelUtil<DsitemDto> util = new ExcelUtil<DsitemDto>(DsitemDto.class);
        return util.exportExcel(list, "作品数据");
    }

    /**
     * 新增保存作品
     */
    @RequiresPermissions("system:dsitem:insert")
    @Log(title = "作品", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    @ResponseBody
    public AjaxResult insert(@RequestBody DsitemDto dsitemDto)
    {
        dsitemDto.setCreateBy(getLoginName());
        dsitemDto.setCreateTime(DateUtils.getNowDate());
        // 将作品信息插入 dsitem 表
        Dsitem dsitem = DsitemConvert.INSTANCE.dsitemDto2dsitemDo(dsitemDto);
        // 获取生成的主键id
        Long itemId = dsitemService.insertDsitem(dsitem);

        // 将作品子集信息插入 dsitem_sub_list 表
        List<DsitemSubList> dsitemSubList = dsitemDto.getDsitemSubLists();
        if(!dsitemSubList.isEmpty()) {
            dsitemSubList.forEach(list -> list.setItemId(itemId));
            dsitemSubListService.insertDsitemSubList(dsitemSubList);
        }

        return AjaxResult.success();
    }

    /**
     * 修改保存作品
     */
    @RequiresPermissions("system:dsitem:edit")
    @Log(title = "作品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody DsitemDto dsitemDto)
    {
        dsitemDto.setUpdateBy(getLoginName());
        dsitemDto.setUpdateTime(DateUtils.getNowDate());
        // 更新 dsitem 表
        Dsitem dsitem = DsitemConvert.INSTANCE.dsitemDto2dsitemDo(dsitemDto);
        // 获取生成的主键id
        dsitemService.updateDsitem(dsitem);

        // 删除 dsitem_sub_list 表的旧数据
        dsitemSubListService.deleteDsitemSubListBySubId(dsitemDto.getItemId());

        // 将作品子集信息插入 dsitem_sub_list 表
        List<DsitemSubList> dsitemSubList = dsitemDto.getDsitemSubLists();
        if(!dsitemSubList.isEmpty()) {
            dsitemSubList.forEach(list -> list.setItemId(dsitemDto.getItemId()));
            dsitemSubListService.insertDsitemSubList(dsitemSubList);
        }

        return AjaxResult.success();
    }

    /**
     * 根据itemId删除记录
     */

    @RequiresPermissions("system:dsitem:remove")
    @Log(title = "作品", businessType = BusinessType.DELETE)
    @PostMapping( "/delete")
    @ResponseBody
    public AjaxResult delete(Long itemId)
    {
        dsitemService.deleteDsitemByItemId(itemId);
        dsitemSubListService.deleteDsitemSubListBySubId(itemId);
        return AjaxResult.success();
    }

    /**
     * 批量删除作品
     */
    @RequiresPermissions("system:dsitem:remove")
    @Log(title = "作品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String itemIds)
    {
        dsitemService.deleteDsitemByItemIds(itemIds);
        dsitemSubListService.deleteDsitemSubListBySubIds(itemIds);
        return AjaxResult.success();
    }

    /**
     * 查看详情
     */
    @RequiresPermissions("system:dsitem:info")
    @GetMapping("/info")
    @ResponseBody
    public AjaxResult info(Long itemId)
    {
        DsitemDto dsitemDto = dsitemService.selectDsitemByItemId(itemId);
        List<DsitemSubList> dsitemSubList = dsitemSubListService.selectDsitemSubListBySubId(itemId);
        dsitemDto.setDsitemSubLists(dsitemSubList);
        return AjaxResult.success(dsitemDto);
    }

    /**
     * 阅读数 +1
     */
    @RequiresPermissions("system:dsitem:info")
    @GetMapping("/pageview")
    @ResponseBody
    public AjaxResult pageview(Long itemId)
    {
        return toAjax(dsitemService.pageview(itemId));
    }
}
